//
//  Copyright (C) 2011 Ray Wang(fangshi168775@hotmail.com).  All Rights Reserved.
//
//  This is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This software is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this software; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
//  USA.
//

package ray.pcvnccontroler;

import java.io.IOException;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.Toast;
import android.widget.EditText;

public class InputDialog extends Dialog {
	
	private TxtSender txtSnd;
	private Context context;
	
	public InputDialog(Context context, RfbProto rfb)
	{
		super(context, android.R.style.Theme);
		setOwnerActivity((Activity)context);
		this.context = context;
		txtSnd = new TxtSender(rfb);
	}
	
	protected void onCreate(Bundle savedInstanceState) 
	{
		Button btn;
		EditText enterText;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.input);
		setTitle(R.string.input);
	
		btn = (Button) findViewById(R.id.send);
		
		btn.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				EditText txt = (EditText)findViewById(R.id.textEnterText);
				try {
					txtSnd.SendText(txt.getText().toString());
					txt.setText(null);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		});
		
		enterText = (EditText)findViewById(R.id.textEnterText);
		enterText.setOnKeyListener(new View.OnKeyListener()
		{

			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				switch(keyCode)
				{
				case KeyEvent.KEYCODE_DEL:
				{
					EditText enterText;
					enterText = (EditText)findViewById(R.id.textEnterText);
					if(enterText.length() <= 0)
					{
						try {
							txtSnd.DeleteText();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
				default:
				}
				return false;
			}
			
		});
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		switch(keyCode)
		{
		case KeyEvent.KEYCODE_BACK:
			cancel();
			return true;
		}
		return false;
	}
	
	public boolean onKeyUp(int keyCode, KeyEvent event)
	{
		
		return false;
	}
}
